package com.Dao;

import com.entity.User;
import com.entity.Userinfo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

/**
 * Created by Administrator on 2017/3/11.
 * 访问用户表和用户信息表的相关操作.
 */
public class UserDao {
    private Session session;
    private SessionFactory sessionFactory;
    Transaction tran=null;
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    private Session getSession(){
        return sessionFactory.openSession();
    }
    //用户注册时，将注册信息插入用户表和用户信息表,校验工作不在本层做
    public void addUser(User user, Userinfo userinfo){
        session=getSession();
        try {
            tran = session.beginTransaction();
            //存入对象
            session.save(user);
            session.save(userinfo);
            tran.commit();
        }catch (Exception e){
            //事务回滚
            if(tran!=null) tran.rollback();
            e.printStackTrace();
        }
    }

    //查找看看某个用户是否存在
    public boolean searchAccount(String account){
        session=getSession();
        User user=(User)session.get(User.class,account);
        session.close();
        if(user==null){ return false;}
        else return true;
    }
    //查找并返回某个用户（假定该用户已经存在，不存在的用户不调用这个方法）的权限
    public Integer getPrivilege(String account){
        session=getSession();
        User user=(User)session.get(User.class,account);
        session.close();
        return user.getPrivilege();
    }

    //查找并返回某个用户（假定该用户已经存在，不存在的用户不调用这个方法）的密码
    public String getPassword(String account){
        session=getSession();
        User user=(User)session.get(User.class,account);
        session.close();
        return user.getPassword();
    }

    //查找并返回某个用户（假定该用户已经存在，不存在的用户不调用这个方法）的超级密码
    public String getSuperPassword(String account){
        session=getSession();
        User user=(User)session.get(User.class,account);
        session.close();
        return user.getSuperpassword();
    }

    //查找并返回某个用户所有信息
    public Userinfo getUserInfo(String account){
        session=getSession();
        Userinfo user=(Userinfo)session.get(Userinfo.class,account);
        return user;
    }

    //修改用户密码
    public boolean alterPassword(String account,String password){
        session=getSession();
        User user=(User)session.get(User.class,account);
        user.setPassword(password.trim());
        try{
            tran=session.beginTransaction();
            session.update(user);
            tran.commit();
        }catch(Exception e){
            if(tran!=null) tran.rollback();
            e.printStackTrace();
            return false;
        }finally {
            session.close();
        }
        return true;
    }

    //修改超级密码
    public boolean alterSuperPassword(String account,String superpassword){
        session=getSession();
        User user=(User)session.get(User.class,account);
        user.setSuperpassword(superpassword.trim());
        try{
            tran=session.beginTransaction();
            session.update(user);
            tran.commit();
        }catch(Exception e){
            if(tran!=null) tran.rollback();
            e.printStackTrace();
            return false;
        }finally {
            session.close();
        }
        return true;
    }

    //修改用户信息
    public boolean alterUserInfo(Userinfo userinfo){
        session=getSession();
        Userinfo user=(Userinfo)session.get(Userinfo.class,userinfo.getAccount());
        user.setAddress(userinfo.getAddress());
        user.setBirthday(userinfo.getBirthday());
        user.setSex(userinfo.getSex());
        user.setName(userinfo.getName());
        user.setIdCard(userinfo.getIdCard());
        user.setCellphone(userinfo.getCellphone());
        user.setEducation(userinfo.getEducation());
        try{
            tran=session.beginTransaction();
            session.update(user);
            tran.commit();
        }catch(Exception e){
            if(tran!=null) tran.rollback();
            e.printStackTrace();
            return false;
        }finally {
            session.close();
        }
        return true;
    }

}
